<?php

class sms_class
{
	const API_URL = 'http://guangdong.ums86.com:8899/sms/Api/Send.do';
	const SP_CODE = '209371';
	const LOGIN_NAME = 'wayne';
	const PASSWORD = 'seatwe2014';
	
	public static function get_instance()
	{
		static $_instance;
		if (is_object($_instance)) {
			return $_instance;
		}
		$_instance = new sms_class();
		return $_instance;
	}
	
	/**
	 * 发送消息
	 * @param int $user_number 手机号码
	 * @param string $content 内容
	 */
	public function send($user_number, $content)
	{
		$ret = false;
		if (empty($user_number)) {
			return $ret;
		}
		
		$params = array(
	        'SpCode' => self::SP_CODE,
	        'LoginName' => self::LOGIN_NAME,
	        'Password' => self::PASSWORD,
			'MessageContent' => iconv("UTF-8", "GB2312//IGNORE", $content),
			'UserNumber' => $user_number,
	        'SerialNumber' => '',
	        'ScheduleTime' => '',
	        'f' => '1'
		);
		
		$ch = curl_init(self::API_URL);
		$options = array(
			CURLOPT_POST => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_POSTFIELDS => http_build_query($params)
		);
		curl_setopt_array($ch, $options);
		parse_str(curl_exec($ch), $response);
		curl_close($ch);
		if (!$response) {
			return $ret;
		}
		
		$response['description'] = iconv("GB2312", "UTF-8", $response['description']);
		if ($response['result'] == 0) {
		    $ret = true;
		    Yii::app()->cache->set($user_number, $content, 60);
		}
		return $ret;
	}
	
	public function create_code()
	{
		$code = mt_rand(100000, 999999);
		return $code;
	}
}